home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / pinfocom_3_0.lha / Source / init.c < prev    next >
C/C++ Source or Header  |  1992-10-22  |  5KB  |  181 lines

  1. /* init.c
  2.  *
  3.  *  ``pinfocom'' -- a portable Infocom Inc. data file interpreter.
  4.  *  Copyright (C) 1987-1992  InfoTaskForce
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; see the file COPYING.  If not, write to the
  18.  *  Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  * $Header: RCS/init.c,v 3.0 1992/10/21 16:56:19 pds Stab $
  23.  */
  24.  
  25. #include "infocom.h"
  26.  
  27.  
  28. void
  29. init()
  30. {
  31.     extern word     resident_blocks;
  32.     extern word     save_blocks;
  33.     extern header_t data_head;
  34.     extern file_t   file_info;
  35.     extern byte     *base_ptr;
  36.     extern byte     *base_end;
  37.     extern byte     *vocab;
  38.     extern byte     *global_ptr;
  39.     extern byte     *common_word_ptr;
  40.     extern byte     *end_res_p;
  41.     extern word     *stack_base;
  42.  
  43.     extern byte     *wsbf_strt;
  44.     extern char     ws_table[];
  45.     extern char     table[];
  46.     extern byte     *end_of_sentence;
  47.     extern word     vocab_entry_size;
  48.     extern word     num_vocab_words;
  49.     extern byte     *strt_vocab_table;
  50.     extern byte     *end_vocab_table;
  51.  
  52.     unsigned long   i;
  53.     word            num;
  54.     byte            *p, *q;
  55.  
  56.     read_header(&data_head);
  57.  
  58.     resident_blocks = data_head.resident_bytes / BLOCK_SIZE;
  59.     if (data_head.resident_bytes % BLOCK_SIZE)
  60.         ++resident_blocks;
  61.  
  62.     i = data_head.verify_length;
  63.     switch (data_head.z_version)
  64.     {
  65.         case 6: i *= 2;
  66.         case 5:
  67.         case 4: i *= 2;
  68.         default:i *= 2;
  69.     }
  70.     file_info.pages = i / BLOCK_SIZE;
  71.     file_info.offset = i % BLOCK_SIZE;
  72.  
  73.     /*
  74.      * Try to calculate how much resident storage we'll need.  We need
  75.      * enough for the resident blocks, pluse the stack, plus any extra
  76.      * whitespace characters (say 100 bytes: way too much but...)
  77.      */
  78.     i = (resident_blocks * BLOCK_SIZE) + STACK_SIZE + 100;
  79.     base_ptr = (byte *)xmalloc(i);
  80.     base_end = base_ptr + i;
  81.  
  82.     /*
  83.      * Load resident memory
  84.      */
  85.     load_page(0, resident_blocks, base_ptr);
  86.  
  87.     /*
  88.      * Set up pointers into resident storage, and information related
  89.      * to it.
  90.      */
  91.     global_ptr = base_ptr + data_head.variable_o;
  92.  
  93.     common_word_ptr = base_ptr + data_head.common_word_o;
  94.  
  95.     save_blocks = data_head.save_bytes / BLOCK_SIZE;
  96.     if (data_head.save_bytes % BLOCK_SIZE)
  97.         ++save_blocks;
  98.  
  99.     /*
  100.      * Set up object information.  I don't know why there's an offset
  101.      * before the object offset in the file and the actual start of
  102.      * the object list, but there is.  I found the correct values by
  103.      * writing a little loop that tried each value incrementally until
  104.      * one worked! :-)
  105.      */
  106.     objd.obj_base = base_ptr + data_head.object_o;
  107.     if (data_head.z_version > 3)
  108.     {
  109.         objd.obj_size = 14;
  110.         objd.obj_offset = 0x70;
  111.         objd.is_eobj = 1;
  112.     }
  113.     else
  114.     {
  115.         objd.obj_size = 9;
  116.         objd.obj_offset = 0x35;
  117.         objd.is_eobj = 0;
  118.     }
  119.  
  120.     /*
  121.      * If we have alternate alphabets, then load them in.
  122.      */
  123.     if (data_head.alphabet_o != 0)
  124.     {
  125.         word    page;
  126.         word    offset;
  127.  
  128.         page = data_head.alphabet_o / BLOCK_SIZE;
  129.         offset = data_head.alphabet_o % BLOCK_SIZE;
  130.  
  131.         for (i = 0; i < 3 * 26; ++i)
  132.             table[i] = get_byte(&page, &offset);
  133.     }
  134.  
  135.     /*
  136.      * Now set up information that comes after the resident storage,
  137.      * such as the stack and the whitespace list.
  138.      */
  139.     end_res_p = base_ptr + (resident_blocks * BLOCK_SIZE);
  140.  
  141.     stack_base = (word *)(end_res_p + STACK_SIZE);
  142.  
  143.     wsbf_strt = (byte *)stack_base;
  144.  
  145.     /*
  146.      * Set up the vocabulary information: first read in the
  147.      * end-of-sentence punctuation marks, then get the size of each
  148.      * vocabulary entry and the number of words in it, and mark the
  149.      * start and end of the vocab table.
  150.      */
  151.     vocab = base_ptr + data_head.vocab_o;
  152.     p = vocab;
  153.     num = Z_TO_BYTE_I(p);
  154.     q = wsbf_strt;
  155.     while (num-- > 0)
  156.         *q++ = *p++;
  157.     end_of_sentence = q;
  158.  
  159.     vocab_entry_size = Z_TO_BYTE_I(p);
  160.     num_vocab_words = Z_TO_WORD_I(p);
  161.  
  162.     strt_vocab_table = p;
  163.     end_vocab_table = strt_vocab_table +
  164.         (vocab_entry_size * (num_vocab_words-1));
  165.  
  166.     p = (byte *)ws_table;
  167.     while (*p != 0)
  168.     {
  169.         *q++ = *p++;
  170.     }
  171.     *q = 0;
  172.  
  173.     /*
  174.      * Set up the page table, random number generator, and print
  175.      * buffers.
  176.      */
  177.     pg_init();
  178.     seed_random();
  179.     print_init();
  180. }
  181.